﻿using System;
using System.Data;
using System.Windows.Forms;

namespace Gestión_UTN.View.general
{
    public partial class Paises : Form
    {
        private static Gestión_UTN.bl.paisbl createPais = Gestión_UTN.factories.blfactory.createPais();

        public Paises()
        {
            InitializeComponent();
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dgvPaises.Rows.Count > 0)
                {
                    bool ingresado = false;

                    foreach (DataGridViewRow dr in dgvPaises.Rows)
                    {
                        if (null != dr.Cells[0].Value)
                        {
                            bo.Paisbo p = createPais.FindPaisByName(dr.Cells[0].Value.ToString());

                            if (null == p)
                            {
                                ingresado = createPais.SavePais(dr.Cells[0].Value.ToString());
                            }
                        }
                    }

                    if (ingresado)
                    {
                        MessageBox.Show("Pais Ingresado!!", "Exito", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        this.Close();

                        this.Dispose();
                    }
                }
            }
            catch (exceptions.blexception)
            {
                MessageBox.Show("Error al Intentar Guardar!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error al Intentar Guardar!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Gestión_UTN.util.EscribirLog.escribirArchivo(ex.Message);
            }
        }

        private void AltaPais_Load(object sender, EventArgs e)
        {
            DataTable paises = createPais.FindAllPaises();

            if (null != paises)
            {
                foreach (DataRow dr in paises.Rows)
                {
                    this.dgvPaises.Rows.Add(dr[1].ToString());
                }
            }
        }
    }
}
